package servlet;

import dao.EmployeeDAOImpl;
import pojo.Employee;
import utils.ApiResult;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@WebServlet("/employee")
public class EmployeeServlet extends BaseServlet {

    // 添加员工
    protected void addEmployee(HttpServletRequest req, HttpServletResponse resp) throws Exception {
//        int idNumber = Integer.parseInt(req.getParameter("idNumber"));
        int companyID = Integer.parseInt(req.getParameter("companyID"));
        String employeeEmail = req.getParameter("employeeEmail");
        String employeeName = req.getParameter("employeeName");
        int employeeAge = Integer.parseInt(req.getParameter("employeeAge"));
        String employeeGender = req.getParameter("employeeGender");
        String employeePhoneNumber = req.getParameter("employeePhoneNumber");

        Employee employee = new Employee(null, companyID, employeeEmail, employeeName, employeeAge, employeeGender, employeePhoneNumber);
        EmployeeDAOImpl.add(employee);
        sendJsonResponse(resp, new ApiResult(200, "Employee added successfully.", null));
    }

    // 删除员工
    protected void deleteEmployee(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int idNumber = Integer.parseInt(req.getParameter("idNumber"));
        EmployeeDAOImpl.delete(idNumber);
        sendJsonResponse(resp, new ApiResult(200, "Employee deleted successfully.", null));
    }

    // 更新员工
    protected void updateEmployee(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int idNumber = Integer.parseInt(req.getParameter("idNumber"));
        int companyID = Integer.parseInt(req.getParameter("companyID"));
        String employeeEmail = req.getParameter("employeeEmail");
        String employeeName = req.getParameter("employeeName");
        int employeeAge = Integer.parseInt(req.getParameter("employeeAge"));
        String employeeGender = req.getParameter("employeeGender");
        String employeePhoneNumber = req.getParameter("employeePhoneNumber");

        Employee employee = new Employee(idNumber, companyID, employeeEmail, employeeName, employeeAge, employeeGender, employeePhoneNumber);
        EmployeeDAOImpl.update(employee);
        sendJsonResponse(resp, new ApiResult(200, "Employee updated successfully.", null));
    }

    // 获取所有员工
    protected void getAllEmployees(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        List<Employee> employees = EmployeeDAOImpl.getAll();
        sendJsonResponse(resp, new ApiResult(200, "Success", employees));
    }

    // 根据身份证号获取员工
    protected void getEmployeeById(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int idNumber = Integer.parseInt(req.getParameter("idNumber"));
        Employee employee = EmployeeDAOImpl.getById(idNumber);
        sendJsonResponse(resp, new ApiResult(200, "Success", employee));
    }

    // 获取员工数量
    protected void getEmployeeCount(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int count = EmployeeDAOImpl.getCount();
        sendJsonResponse(resp, new ApiResult(200, "Success", count));
    }

    // 分页获取员工
    protected void getEmployeesByPage(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int start = Integer.parseInt(req.getParameter("start"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));
        int offset = (start - 1) * pageSize;
        List<Employee> employees = EmployeeDAOImpl.getByPage(offset, pageSize);
        sendJsonResponse(resp, new ApiResult(200, "Success", employees));
    }

    // 根据关键字搜索员工
    protected void findEmployeesByKeyword(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String keyword = req.getParameter("keyword");
        List<Employee> employees = EmployeeDAOImpl.findByKeyword(keyword);
        sendJsonResponse(resp, new ApiResult(200, "Success", employees));
    }

    // 根据关键字分页搜索员工
    protected void findEmployeesByPageAndKeyword(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String keyword = req.getParameter("keyword");
        int start = Integer.parseInt(req.getParameter("start"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));
        int offset = (start - 1) * pageSize;
        List<Employee> employees = EmployeeDAOImpl.findByPageAndKeyword(offset, pageSize, keyword);
        sendJsonResponse(resp, new ApiResult(200, "Success", employees));
    }
}
